package com.ibm.crl.monitor.client;

import java.util.HashMap;
import java.util.Map;

import org.apache.wink.client.Resource;
import org.apache.wink.client.RestClient;
import org.json.JSONArray;
import org.json.JSONObject;

import com.ibm.crl.monitor.util.ParameterUtils;

public class GraphiteClient {
/*
	public static JSONObject fetchRequest(Map<String , String> map){
		RestClient rc = new RestClient();
		Resource resource = rc.resource(ParameterUtils.graphiteURL);
		resource.queryParam("target", CAClient.getTarget(map.get("metric")));
		resource.queryParam("from", map.get("from"));
		resource.queryParam("until", map.get("until"));
		resource.queryParam("format", "json");
		JSONObject o = resource.get(JSONObject.class);
		
		System.out.println(o.toString());
		return o;
	}
*/	

	public static JSONArray fetchRequest(Map<String , String> map){
		RestClient rc = new RestClient();
		Resource resource = rc.resource(ParameterUtils.graphiteURL);
		resource.queryParam("target", CAClient.getTarget("")+"."+map.get("metric")+".*");
		resource.queryParam("from", map.get("from"));
		resource.queryParam("until", map.get("until"));
		resource.queryParam("format", "json");
		
		JSONArray ja = resource.get().getEntity(JSONArray.class);
		
		return ja;
	}
	
	public static void main(String [] argv){
		Map<String, String> map = new HashMap<String, String>();
		map.put("metric", "cpu");
		map.put("from", "-10min");
		map.put("until", "");
		map.put("format", "json");
		fetchRequest(map);
	}
}
